package com.rh.controller;


import com.rh.entity.UcenterMember;
import com.rh.entity.UcenterMemberOrderVo;
import com.rh.entity.vo.UcenterRegisterVo;
import com.rh.result.ResultVo;
import com.rh.service.UcenterMemberService;
import com.rh.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 会员表 前端控制器
 */
@Api(tags = "会员管理")
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;

    /**
     * 登陆
     */
    @ApiOperation(value = "1.登陆")
    @PostMapping("login")
    public ResultVo login(@RequestBody UcenterMember ucenterMember){
        //调用service方法实现登陆
        //返回token值
        String token=ucenterMemberService.login(ucenterMember);
        return ResultVo.ok().data("token",token);
    }
    /**
     * 注册
     */
    @ApiOperation(value = "2.注册")
    @PostMapping("register")
    public ResultVo register(@RequestBody UcenterRegisterVo ucenterRegisterVo){
        ucenterMemberService.register(ucenterRegisterVo);
        return ResultVo.ok();
    }
    /**
     * 根据token获取用户信息
     */
    @ApiOperation(value = "3.根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public ResultVo getToken(HttpServletRequest httpServletRequest){
        //使用jwt工具获取id值
        String id = JwtUtils.getMemberIdByJwtToken(httpServletRequest);
        //根据id查询数据库
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        System.out.println("根据token查询出来的数据是:"+ucenterMember);
        return ResultVo.ok().data("userInfo",ucenterMember);
    }
    //根据用户id查询用户信息
    @ApiOperation(value = "4.根据用户id获取用户信息")
    @PostMapping("/getMemberInfoById/{id}")
    public UcenterMemberOrderVo getMemberInfoById(@PathVariable String id){
       UcenterMember member=ucenterMemberService.getById(id);
       //把member对象的值赋值给UcenterMemberOrderVo对象
        UcenterMemberOrderVo ucenterMemberOrderVo = new UcenterMemberOrderVo();
        BeanUtils.copyProperties(member,ucenterMemberOrderVo);
        return ucenterMemberOrderVo;
    }
    /**
     * 查询某一天的注册人数
     */
    @ApiOperation(value = "5.查询某一天的注册人数")
    @GetMapping("countRegister/{day}")
    public ResultVo countRegister(@PathVariable String day){
        Integer count=ucenterMemberService.countRegisterDay(day);
        return ResultVo.ok().data("countRegister",count);
    }
}

